/*
 * Copyright 2008 Neil Bacon
 * Distributed under the terms of the Mozilla Public License version 1.1
 * or the GNU General Public License version 3.
 */

package nb.xadd.operator.impl;

import nb.xadd.node.Node;
import nb.xadd.operator.UnaryOperator;

/**
 * Key for cache of: (unaryOperator, node) -> result
 */
public class UnaryOperatorCacheKey {
    private final UnaryOperator unaryOperator;
    private final Node node;
    private final int hashCode;

    public UnaryOperatorCacheKey(UnaryOperator unaryOperator, Node node) {
        this.unaryOperator = unaryOperator;
        this.node = node;
        this.hashCode = calcHashCode();
    }

    @Override
    public int hashCode() {
        return hashCode;
    }

    private int calcHashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + node.hashCode();
        result = prime * result + unaryOperator.hashCode();
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        UnaryOperatorCacheKey other = (UnaryOperatorCacheKey) obj;
        return node == other.node && unaryOperator.equals(other.unaryOperator);
        // Only same objects are equal because Nodes are cached/interned
    }


}
